package com.example.vaultmysql.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 数据库凭证DTO
 * 
 * @author System
 * @version 1.0.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DatabaseCredentials {

    /**
     * 数据库用户名
     */
    private String username;

    /**
     * 数据库密码
     */
    private String password;

    /**
     * Vault路径
     */
    private String vaultPath;

    /**
     * 凭证获取时间戳
     */
    private Long timestamp;

    /**
     * 凭证是否有效
     */
    private Boolean valid;

    /**
     * 构造函数
     */
    public DatabaseCredentials(String username, String password, String vaultPath) {
        this.username = username;
        this.password = password;
        this.vaultPath = vaultPath;
        this.timestamp = System.currentTimeMillis();
        this.valid = true;
    }

    /**
     * 检查凭证是否有效
     */
    public boolean isValid() {
        return valid != null && valid && 
               username != null && !username.trim().isEmpty() &&
               password != null && !password.trim().isEmpty();
    }

    /**
     * 获取凭证年龄（毫秒）
     */
    public long getAge() {
        if (timestamp == null) {
            return 0;
        }
        return System.currentTimeMillis() - timestamp;
    }

    /**
     * 检查凭证是否过期
     */
    public boolean isExpired(long maxAgeMs) {
        return getAge() > maxAgeMs;
    }

    /**
     * 标记凭证为无效
     */
    public void markInvalid() {
        this.valid = false;
    }

    /**
     * 获取安全的字符串表示（不包含密码）
     */
    public String toSafeString() {
        return String.format("DatabaseCredentials{username='%s', vaultPath='%s', valid=%s, age=%dms}", 
                           username, vaultPath, valid, getAge());
    }

    @Override
    public String toString() {
        return toSafeString();
    }
}